/**
 * 
 */
package cn.gov.msa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.dao.DeclareItemMapper;
import cn.gov.msa.dao.DeclareItemMaterialMapper;
import cn.gov.msa.dao.FileMapper;
import cn.gov.msa.dao.ServiceGuideMapper;
import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.DeclareItemMaterial;
import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.model.File;
import cn.gov.msa.model.ServiceGuide;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.DeclareItemService;
import cn.gov.msa.util.InfoObjUtil;
import cn.gov.msa.util.ObjectUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class DeclareItemServiceImpl implements DeclareItemService {

	@Autowired
	private DeclareItemMapper declareItemMapper;
	@Autowired
	private DeclareItemMaterialMapper declareItemMaterialMapper;
	@Autowired
	private ServiceGuideMapper serviceGuideMapper;	
	@Autowired
	private FileMapper fileMapper;
	
	
	@Override
	public List<DeclareItem> selectData(int pageNum, int pageSize, DeclareItem declareItem) throws Exception {

		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码
        
        //读取数据库信息
	    List<DeclareItem> declareItemList = declareItemMapper.selectData(declareItem);

	    return declareItemList;
	}
	
	@Override
	public DeclareItem getInfoByItemno(String itemno) throws Exception {
        
        //读取数据库信息
	    DeclareItem declareItem = declareItemMapper.selectByPrimaryKey(itemno);

	    return declareItem;
	}
	
	@Transactional
	public boolean updateData(DeclareItem declareItem, List<DeclareItemMaterial> declareItemMaterialList) throws Exception {
		boolean result = false;
		
		int i = declareItemMapper.updateByPrimaryKeySelective(declareItem);
		
		declareItemMaterialMapper.deleteByItemno(declareItem.getItemno());
		
		if(declareItemMaterialList != null){
			for (DeclareItemMaterial declareItemMaterial : declareItemMaterialList) {
				declareItemMaterial.setItemno(declareItem.getItemno());
				declareItemMaterialMapper.insert(declareItemMaterial);
			}
		}
		
		if(i > 0)
			result = true;
		
		return result;
	}

	@Transactional
	public boolean saveData(DeclareItem declareItem, List<DeclareItemMaterial> declareItemMaterialList) throws Exception {
		boolean result = false;
		
		int idict = declareItemMapper.insert(declareItem);
		
		declareItemMaterialMapper.deleteByItemno(declareItem.getItemno());
		
		for (DeclareItemMaterial declareItemMaterial : declareItemMaterialList) {
			declareItemMaterial.setItemno(declareItem.getItemno());
			int ideclareItemMaterial = declareItemMaterialMapper.insert(declareItemMaterial);
		}
		
		if(idict > 0)
			result = true;
		
		return result;
	}
	

	@Transactional
	public int delete(String itemno) throws Exception {

		int material = declareItemMaterialMapper.deleteByItemno(itemno);
		
		int result = declareItemMapper.deleteByPrimaryKey(itemno);
		
		
		return result;
	}

	@Override
	public ServiceGuide selectByPrimaryKey(String sgid) {
		// TODO Auto-generated method stub
		return serviceGuideMapper.selectByPrimaryKey(sgid);
	}

	@Override
	public File selectFile(String fileId) {
		// TODO Auto-generated method stub
		return fileMapper.selectFile(fileId);
	}
}
